﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_ORDER.BizLogic;
using ERP_RORZE_ORDER.Common;
using ERP_RORZE_ORDER.Common.ValueObject;
using ERP_RORZE_ORDER.OrderObject.UserControls;
using ERP_RORZE_PERMISSION.Base;
using MakerHelper = ERP_RORZE_ORDER.BizLogic.MakerHelper;
using Utilities = ERP_RORZE_TOOL.Common.Utilities;

namespace ERP_RORZE_ORDER.OrderObject.Forms
{
    public partial class frm_UnitAdd : DevExpress.XtraEditors.XtraForm
    {
        private LogUtils log;
        private ucUnit parent;

        public frm_UnitAdd(ucUnit ucP)
        {
            InitializeComponent();
            parent = ucP;
            this.CancelButton = this.btCancel;
            log = new LogUtils();
            this.ActiveControl = txtUnitID;
        }

        private void frm_ProductAdd_Load(object sender, EventArgs e)
        {
           
        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            UnitHelper helper = new UnitHelper(new Unit());
            if (GetData(helper.ValueObject))
            {
                if (helper.Insert() > 0)
                {

                    //write Log
                    log.Table = LogConstant.Table.F2Order.Unit;
                    log.Task = LogConstant.Task.Insert;
                    log.Description = "UnitID: " + helper.ValueObject.UnitID + "; UnitName: " + helper.ValueObject.UnitName;
                    log.WriteLog();

                    if (Utilities.MessageConfirmationYesNo(OrderConstants.Message.AddSuccessAndQuestion))
                    {

                        txtUnitID.Text ="";
                        txtUnitName.Text="";
                        txtUnitID.Focus();
                    }
                    else
                    {
                        this.Close();
                    }
                }
                else
                {

                    Utilities.MessageErrorOk(OrderConstants.Message.AddFailed);
                }
                parent._action = 0;
                parent.BindDatatoGrid();
            }
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        #region Private Method
        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == 0x0100 && (int)m.WParam == 13)
            {
                this.ProcessTabKey(true);
            }
            return base.ProcessKeyPreview(ref m);
        }

        private bool GetData(Unit o)
        {
            #region Check valid

            if (Utilities.IsNullOrEmpty(txtUnitID.Text.Trim()))
            {
                MessageBox.Show(OrderConstants.Message.Unit.UnitIDCantEmpty, OrderConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(txtUnitName.Text.Trim()))
            {
                MessageBox.Show(OrderConstants.Message.Unit.UnitNameCantEmpty, OrderConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (CheckDuplicate(txtUnitID.Text.Trim()))
            {
                MessageBox.Show(OrderConstants.Message.Unit.UnitIDHasExisted, OrderConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            #endregion

            o.UnitID = txtUnitID.Text;
            o.UnitName = txtUnitName.Text;

            return true;
        }


        private bool CheckDuplicate(string productId)
        {
            UnitHelper helper = new UnitHelper(new Unit());
            helper.ValueObject.UnitID = productId;
            DataTable dt = helper.GetOne();
            if (dt.Rows != null && dt.Rows.Count > 0)
                return true;
            return false;

        }
        #endregion

        private void frm_UnitAdd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Alt)
            {
                if (e.KeyCode == Keys.D)
                {
                    this.btAdd.PerformClick();
                }
            }
            else if (e.KeyCode == Keys.End)
            {
                btAdd.Focus();
            }
            else if (e.KeyCode == Keys.Home)
            {
                txtUnitID.Focus();
            }
        }

    }
}